//
//  CROneSentenceCollectionViewCell.m
//  NewAge
//
//  Created by 王鑫 on 15/11/30.
//  Copyright © 2015年 ColorRevolution. All rights reserved.
//

#import "CROneSentenceCollectionViewCell.h"

@implementation CROneSentenceCollectionViewCell

#pragma mark Method init

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
    }
    return self;
}



#pragma mark - Method createView

- (void)createView {
    self.volLabel = [[UILabel alloc] init];
    self.volLabel.font = [UIFont fontWithName:@"ArialUnicodeMS" size:15];
    [self.contentView addSubview:self.volLabel];
    
    self.imageAuthorLabel = [[UILabel alloc] init];
    self.imageAuthorLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
    self.imageAuthorLabel.textColor = [UIColor colorWithRed:0.573f green:0.573f blue:0.573f alpha:1.00f];
    [self.contentView addSubview:self.imageAuthorLabel];
    
    self.imageTitleLabel = [[UILabel alloc] init];
    self.imageTitleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
    self.imageTitleLabel.textColor = [UIColor colorWithRed:0.573f green:0.573f blue:0.573f alpha:1.00f];
    [self.contentView addSubview:self.imageTitleLabel];
    
    self.pimageView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.pimageView];
    
    self.dayLabel = [[UILabel alloc] init];
    self.dayLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:39];
    self.dayLabel.textColor = [UIColor colorWithRed:0.965f green:0.463f blue:0.220f alpha:0.50f];
    [self.contentView addSubview:self.dayLabel];
    
    self.monthLabel = [[UILabel alloc] init];
    self.monthLabel.font = [UIFont fontWithName:@"ArialUnicodeMS" size:12];
    self.monthLabel.textColor = [UIColor colorWithRed:0.659f green:0.659f blue:0.659f alpha:1.00f];
    [self.contentView addSubview:self.monthLabel];
    
    self.contentBgView = [[UIView alloc] init];
    self.contentBgView.backgroundColor = [UIColor colorWithRed:0.965f green:0.463f blue:0.220f alpha:0.90f];
    self.contentBgView.layer.cornerRadius = 5;
    [self.contentView addSubview:self.contentBgView];
    
    self.contentLabel = [[UILabel alloc] init];
    self.contentLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
    self.contentLabel.numberOfLines = 0;
    [self.contentLabel sizeToFit];
    self.contentLabel.textColor = [UIColor blackColor];
    [self.contentBgView addSubview:self.contentLabel];
    
    self.likeBgView = [[UIView alloc] init];
    self.likeBgView.layer.borderWidth = 1;
    self.likeBgView.layer.borderColor = [[UIColor colorWithRed:0.804f green:0.804f blue:0.804f alpha:1.00f] CGColor];
    self.likeBgView.layer.cornerRadius = 10 * HEIGHT / 667.0;
    [self.contentView addSubview:self.likeBgView];
    
    self.likeImageView = [[UIImageView alloc] init];
    self.likeImageView.image = [UIImage imageNamed:@"iconfont-xihuanb.png"];
    [self.contentView addSubview:self.likeImageView];
    
    self.likeLabel = [[UILabel alloc] init];
    self.likeLabel.font = [UIFont systemFontOfSize:13];
    self.likeLabel.textColor = [UIColor colorWithRed:0.382f green:0.382f blue:0.382f alpha:1.00f];
    [self.likeBgView addSubview:self.likeLabel];
}



#pragma mark - Method layoutSubviews

- (void)layoutSubviews {
    [super layoutSubviews];
    int padding = 10;
    
    [self.volLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).with.offset(padding);
        make.bottom.equalTo(self.pimageView.mas_top).with.offset(-padding);
        make.left.equalTo(self.contentView.mas_left).with.offset(padding);
        make.height.mas_equalTo(20 / 667.0 * HEIGHT);
    }];
    
    [self.imageAuthorLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.pimageView.mas_bottom).with.offset(padding);
        make.left.equalTo(self.contentView.mas_left).with.offset(padding);
    }];
    
    [self.imageTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.imageAuthorLabel.mas_bottom).with.offset(padding);
        make.left.equalTo(self.contentView.mas_left).with.offset(padding);
    }];
    
    [self.pimageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(300 * HEIGHT / 667.0);
        make.width.mas_equalTo(WIDTH / 375.0 * WIDTH);
    }];
    
    [self.dayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).with.offset(padding);
        make.top.equalTo(self.imageTitleLabel.mas_bottom).with.offset(padding);
    }];
    
    [self.monthLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).with.offset(padding);
        make.top.equalTo(self.dayLabel.mas_bottom).with.offset(3);
        
    }];
    
    [self.contentBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.pimageView.mas_bottom).with.offset(7 * padding);
        make.left.equalTo(self.monthLabel.mas_right).with.offset(padding);
        make.width.mas_equalTo(WIDTH - 103 * WIDTH / 375.0);
        make.height.equalTo(self.contentLabel);
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.pimageView.mas_bottom).with.offset(7 * padding);
        make.left.equalTo(self.monthLabel.mas_right).with.offset(padding);
        make.width.mas_equalTo(WIDTH - 118 * WIDTH / 375.0);
    }];
    
    [self.likeBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentBgView.mas_bottom).with.offset(2 * padding);
        make.left.equalTo(self.contentView.mas_left).with.offset(250 / 375.0 * WIDTH);
        make.width.mas_equalTo(50 * WIDTH / 375.0 + 40 * HEIGHT / 667.0);
        make.height.mas_equalTo(20 * HEIGHT / 667.0);
    }];
    
    [self.likeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentBgView.mas_bottom).with.offset(2 * padding);
        make.left.equalTo(self.likeBgView.mas_left).with.offset(15);
        make.width.mas_equalTo(25 / 667.0 * WIDTH);
        make.height.mas_equalTo(self.likeBgView);
        
    }];
    
    [self.likeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentBgView.mas_bottom).with.offset(2 * padding);
        make.right.equalTo(self.contentBgView).with.offset(-2 * padding);
        make.height.equalTo(self.likeBgView);
    }];

}



#pragma mark - Method setModel

- (void)setModel:(CRSentenceModel *)model {
    if (_model != model) {
        _model = model;
    
        NSRange range = [self.model.strAuthor rangeOfString:@"&"];
        NSString *authorStr = [self.model.strAuthor substringToIndex:range.location];
        NSString *titleStr = [self.model.strAuthor substringFromIndex:range.location + 1];
        NSString *dayStr = [self.model.strMarketTime substringFromIndex:self.model.strMarketTime.length - 2];
        NSString *monthStr = [self.model.strMarketTime substringWithRange:NSMakeRange(self.model.strMarketTime.length - 5, 2)];
        NSString *yearStr = [self.model.strMarketTime substringToIndex:4];
        
        NSInteger month = [monthStr integerValue];
        switch (month) {
            case 1:
                self.monthLabel.text = [NSString stringWithFormat:@"Jan,%@", yearStr];
                break;
            case 2:
                self.monthLabel.text = [NSString stringWithFormat:@"Feb,%@", yearStr];
                break;
            case 3:
                self.monthLabel.text = [NSString stringWithFormat:@"Mar,%@", yearStr];
                break;
            case 4:
                self.monthLabel.text = [NSString stringWithFormat:@"Apr,%@", yearStr];
                break;
            case 5:
                self.monthLabel.text = [NSString stringWithFormat:@"May,%@", yearStr];
                break;
            case 6:
                self.monthLabel.text = [NSString stringWithFormat:@"Jun,%@", yearStr];
                break;
            case 7:
                self.monthLabel.text = [NSString stringWithFormat:@"Jul%@", yearStr];
                break;
            case 8:
                self.monthLabel.text = [NSString stringWithFormat:@"Aug,%@", yearStr];
                break;
            case 9:
                self.monthLabel.text = [NSString stringWithFormat:@"Sep,%@", yearStr];
                break;
            case 10:
                self.monthLabel.text = [NSString stringWithFormat:@"Oct,%@", yearStr];
                break;
            case 11:
                self.monthLabel.text = [NSString stringWithFormat:@"Nov,%@", yearStr];
                break;
            case 12:
                self.monthLabel.text = [NSString stringWithFormat:@"Dec,%@", yearStr];
                break;
                
            default:
                break;
        }

        
        self.volLabel.text = model.strHpTitle;
        self.imageTitleLabel.text = titleStr;
        self.imageAuthorLabel.text = authorStr;
        self.dayLabel.text = dayStr;
        [self.pimageView sd_setImageWithURL:[NSURL URLWithString:model.strThumbnailUrl] placeholderImage:[UIImage imageNamed:@"backWhite.png"]];
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:model.strContent];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
        [paragraphStyle setLineSpacing:6];
        [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, model.strContent.length)];
        self.contentLabel.attributedText = attributedString;
        self.likeLabel.text = model.strPn;

    }
}




@end
